package com.xb.cloud.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.keygen.IPKeyGenerator;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.model.EndUser;
import com.xb.cloud.user.model.EndUserExample;
import com.xb.cloud.user.model.mapper.EndUserMapper;
import com.xb.cloud.user.service.client.EndUserService;
import com.xb.cloud.user.service.client.bo.EndUserBo;

/**
 * 
 * @ClassName: EndUserServiceImpl
 * @Description: 终端用户基础服务类
 * @author: 鲜备科技
 * @date: 2017年12月21日 下午4:30:36
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Service
public class EndUserServiceImpl extends AbstractService implements EndUserService {

	@Resource
	EndUserMapper endUserMapper;

	@Resource
	IPKeyGenerator ipKeyGenerator;

	@Override
	public ServiceReturnInfo createEndUser(EndUserBo endUserBO) {
		if (endUserBO == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		endUserBO.setId(ipKeyGenerator.generateKey());
		EndUser endUser = new EndUser();
		BeanUtils.copyProperties(endUserBO, endUser);

		endUser.setCreateTime(timeService.getDate());
		endUserMapper.insertSelective(endUser);
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo updateByPrimaryKey(EndUserBo endUserBo) {
		EndUser endUser = new EndUser();
		BeanUtils.copyProperties(endUserBo, endUser);

		endUser.setUpdateTime(timeService.getDate());
		endUserMapper.updateByPrimaryKey(endUser);
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo updatePassword(EndUserBo endUserBO) {
		EndUser endUser = new EndUser();
		endUser.setId(endUserBO.getId());
		endUser.setCompanyId(endUserBO.getCompanyId());
		endUser.setEndUserPassword(endUserBO.getEndUserPassword());
		endUser.setUpdateTime(timeService.getDate());
		int updated = endUserMapper.updateByPrimaryKeySelective(endUser);
		return ServiceReturnUtils.success(updated);
	}

	@Override
	public ServiceReturnInfo getEndUserById(Long id) {
		EndUser endUser = endUserMapper.selectByPrimaryKey(id);
		EndUserBo endUserBO = new EndUserBo();
		if (endUser == null) {
			return ServiceReturnUtils.success(null);
		}
		BeanUtils.copyProperties(endUser, endUserBO);
		return ServiceReturnUtils.success(endUserBO);
	}

	@Override
	public ServiceReturnInfo getEndUserByPhoneNum(Long companyId, String phoneNum) {
		EndUserExample example = new EndUserExample();
		example.createCriteria().andPhoneNumEqualTo(phoneNum).andCompanyIdEqualTo(companyId);
		List<EndUser> endUsers = endUserMapper.selectByExample(example);
		if (endUsers.isEmpty()) {
			return ServiceReturnUtils.success(null);
		} else {
			EndUserBo endUserBO = new EndUserBo();
			BeanUtils.copyProperties(endUsers.get(0), endUserBO);
			return ServiceReturnUtils.success(endUserBO);
		}
	}

	@Override
	public ServiceReturnInfo getEndUserByIds(Long companyId, Long endUserId) {
		EndUserExample example = new EndUserExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andIdEqualTo(endUserId);
		List<EndUser> endUsers = endUserMapper.selectByExample(example);
		if (endUsers.isEmpty()) {
			return ServiceReturnUtils.success(null);
		} else {
			EndUserBo endUserBO = new EndUserBo();
			BeanUtils.copyProperties(endUsers.get(0), endUserBO);
			return ServiceReturnUtils.success(endUserBO);
		}
	}

}
